package com.tulei.common.config;

import cn.hutool.core.thread.ThreadFactoryBuilder;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Configuration
@EnableAsync
public class ThreadPoolConfig {

    private int corePoolSize = 8;

    private int maximumPoolSize = 16;

    private int queueCapacity = 10;

    private int keepAliveTime = 600;

    public static ThreadPoolExecutor threadPoolExecutorPre = new ThreadPoolExecutor(8, 16, 600, TimeUnit.SECONDS, new LinkedBlockingQueue<>(),ThreadFactoryBuilder.create().setNamePrefix("tulei").build());


    @Bean(name = "threadPoolStarter")
    public ExecutorService threadPoolStarter(){
        ThreadPoolTaskExecutor threadPoolExecutor = new ThreadPoolTaskExecutor();
        threadPoolExecutor.setCorePoolSize(corePoolSize);
        threadPoolExecutor.setMaxPoolSize(maximumPoolSize);
        threadPoolExecutor.setQueueCapacity(queueCapacity);
        threadPoolExecutor.setKeepAliveSeconds(keepAliveTime);
        threadPoolExecutor.setThreadNamePrefix("test-thread-");
        threadPoolExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        threadPoolExecutor.initialize();
        return threadPoolExecutor.getThreadPoolExecutor();
    }

}
